package cn.itsource.basic.domain;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.MD5;

import java.util.Optional;

/**
 * @author: maizi
 * @Description:
 * @Date: Created in 13:18 2022/2/25
 * @Modified: By:
 */
public interface CreatePassword {

    void setSalt(String salt);

    String getSalt();

    void setPassword(String password);

    String getPassword();

    /**
     * 创建随机密码并返回
     * @return
     */
    default String createPasswordReturn(){
        return RandomUtil.randomNumbers (6);
    }

    /**
     * 创建随机密码并直接set
     */
    default void createPassword(){
        setPassword ( RandomUtil.randomNumbers (6));
    }
    /**
     * 创建随机盐值并返回
     * @return
     */
    default String createSaltReturn(){
        return RandomUtil.randomNumbers (8);
    }
    /**
     * 创建随机盐值并直接set
     * @return
     */
    default void createSalt(){
        this.setSalt( RandomUtil.randomNumbers (8));
    }
    /**
     * 创建 密码加密工具,需要参数
     * @param password 密码
     * @param salt  盐
     * @return
     */
    default String passwordMD5(String password,String salt){
        this.setSalt( Optional.ofNullable (salt).orElse ("1"));
        return MD5.create ().setSalt (salt.getBytes ()).digestHex (password);
    }
    /**
     * 创建 密码加密工具,直接set
     * @return
     */
    default void passwordMD5(){
        //Optional.ofNullable () 判断里面的参数是否为空
        this.setSalt( Optional.ofNullable (this.getSalt ()).orElse ("1"));
        this.setPassword ( MD5.create ().setSalt (this.getSalt ().getBytes ()).digestHex (this.getPassword ()));
    }
}
